﻿#include "calendar.h"
#include "ui_calendar.h"

Calendar::Calendar(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Calendar)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint);
    ui->comboBox->setView(new QListView());
    ui->comboBox_2->setView(new QListView());
    ui->comboBox_3->setView(new QListView());

    //QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this);
    //shadow_effect->setOffset(1, -1);
    //shadow_effect->setColor(QColor(0, 0, 0, 128));
    //shadow_effect->setBlurRadius(3);

//    ui->label_2->setGraphicsEffect(shadow_effect);
//    ui->label_6->setGraphicsEffect(shadow_effect);
//    ui->label_8->setGraphicsEffect(shadow_effect);

    for(int i = 0; i < 42; i++)
    {
        int n = i % 7;
        int m = i / 7;
        m_item[i] = new DayItem(this);
        m_item[i]->move(10 + 55 * n, 80 + 55 * m);
        connect(m_item[i], SIGNAL(myClicked(DayItem*)), this, SLOT(on_day_clicked(DayItem*)));
        //m_item[i]->setDayData("20", QStringLiteral("初一"));
    }

    m_nYear = QDate::currentDate().year();
    m_nMonth = QDate::currentDate().month();
    m_nDay = QDate::currentDate().day();

    //initDate();
    JumpTo(m_nYear, m_nMonth, m_nDay);
}

Calendar::~Calendar()
{
    delete ui;
}

void Calendar::on_day_clicked(DayItem *item)
{
    for(int i = 0; i < 42; i++)
    {
        m_item[i]->setSelected(false);
    }
    if(item->getInMonth() == CUR_MONTH)
    {
        //item->setSelected(true);
        m_nDay = item->getDay();
    }
    else if(item->getInMonth() == PRE_MONTH)
    {
        //上年
        m_nYear = (1 == m_nMonth ? m_nYear - 1 : m_nYear);
        //上月
        m_nMonth = (1 == m_nMonth ? 12 : m_nMonth - 1);
        //上月天数
        m_nDay = item->getDay();

    }
    else if(item->getInMonth() == NXT_MONTH)
    {
        //下年
        m_nYear = (12 == m_nMonth ? m_nYear + 1 : m_nYear);
        //下月
        m_nMonth = (12 == m_nMonth ? 1 : m_nMonth + 1);
        //下月天数
        m_nDay = item->getDay();

    }
    JumpTo(m_nYear, m_nMonth, m_nDay);
}

QByteArray Calendar::HttpRequest(int year, int month, int day)
{
    QByteArray data;
    QString url = QString("http://v.juhe.cn/calendar/day?date=%1-%2-%3").arg(year).arg(month).arg(day);
    url += KeyVal;

    QNetworkAccessManager manager;

    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
    request.setUrl(QUrl(url));

    QNetworkReply *reply = manager.get(request);
    QEventLoop loop;
    connect(&manager, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()));
    loop.exec();

    if(reply->error() != QNetworkReply::NoError)
    {
        qDebug()<<"reply error:"<<reply->errorString();
    }
    else
    {
        data = reply->readAll();
    }
    reply->deleteLater();
    return data;
}


void Calendar::JumpTo(int year, int month, int day)
{
    for(int i = 0; i < 42; i++)
    {
        m_item[i]->Init();
    }

    ui->label->setText(QString("%1-%2-%3").arg(m_nYear).arg(m_nMonth, 2, 10, QChar('0')).arg(m_nDay, 2, 10, QChar('0')));
    ui->comboBox->setCurrentIndex(year - 2000);
    ui->comboBox_2->setCurrentIndex(month - 1);
    ui->label_2->setText(QString::number(m_nDay));
    QString strLuner = Date::getLunarMonAndDay(m_nYear, m_nMonth, m_nDay);
    ui->label_3->setText(strLuner);
    ui->label_4->setText(Date::getLunarTime(m_nYear));

    // 首先判断当前月的第一天是星期几
    int nWeek = Date::getFirstDayOfWeek(year, month);
    int nMonthDays = Date::getMonthDays(year, month);

    //上年
    int nPreYear = (1 == month ? year - 1 : year);
    //上月
    int nPreMonth = (1 == month ? 12 : month - 1);
    //上月天数
    int nPreMonDays = Date::getMonthDays(1 == month ? year - 1 : year, 1 == month ? 12 : month - 1);

    //下年
    int nNextYear = (12 == month ? year + 1 : year);
    //下月
    int nNextMonth = (12 == month ? 1 : month + 1);
    //下月天数

    // 显示上月天数
    int num = nWeek == 0 ? 7 : nWeek;
    for(int i = 0; i < num - 1; i++)
    {
        //QString day = QString::number(nPreYear) + "-" + QString::number(nPreMonth) + "-" + QString::number(nPreMonDays - nWeek + i + 1);
        //bool isHoliday = m_item[i]->parse(HttpRequest(day));
        m_item[i]->setDayData(nPreMonDays - num + i + 2, Date::getLunarDate(nPreYear, nPreMonth, nPreMonDays - num + i + 2));
        m_item[i]->setDayState(NORMAL, (i%7 == 5 || i%7 == 6), PRE_MONTH, Date::isHoliday(nPreYear, nPreMonth, nPreMonDays - num + i + 2), false);
    }

    // 显示下月天数
    //int num = nWeek == 0 ? 7 : nWeek;
    for(int i = (num + nMonthDays - 1); i < 42; i++)
    {
        //QString day = QString::number(nNextYear) + "-" + QString::number(nNextMonth) + "-" + QString::number(i - (nWeek + nMonthDays) + 1);
        //bool isHoliday = m_item[i]->parse(HttpRequest(day));
        m_item[i]->setDayData(i - (num + nMonthDays) + 2, Date::getLunarDate(nNextYear, nNextMonth, i - (num + nMonthDays) + 2));
        m_item[i]->setDayState(NORMAL, (i%7 == 5 || i%7 == 6), NXT_MONTH, Date::isHoliday(nNextYear, nNextMonth, i - (num + nMonthDays) + 2), false);
    }

    //显示当月
    //int index = 0;
    //int num = nWeek == 0 ? 7 : nWeek;
    for(int i = num; i < (nMonthDays + num); i++)
    {
        //index = 0 == nWeek ? (i+7): i;
        //QString day = QString::number(m_nYear) + "-" + QString::number(m_nMonth) + "-" + QString::number(i - nWeek + 1);
        //bool isHoliday = m_item[i]->parse(HttpRequest(day));
        m_item[i-1]->setDayData(i - num + 1, Date::getLunarDate(year, month, i - num + 1));
        m_item[i-1]->setDayState(NORMAL, ((0 == (i % 7)) || (6 == (i% 7))), CUR_MONTH, Date::isHoliday(year, month, i - num + 1), (QDate::currentDate().year() == year) && (QDate::currentDate().month() == month) && QDate::currentDate().day() == (i - num + 1));
        if(day == i - num + 1)
        {
            m_item[i-1]->setSelected(true);
        }
    }

    QByteArray data = HttpRequest(year, month, day);
    QJsonParseError json_error;
    QJsonDocument document = QJsonDocument::fromJson(data, &json_error);
    if(json_error.error == QJsonParseError::NoError)
    {
        if(document.isObject())
        {
            const QJsonObject obj = document.object();
            if(obj.contains("error_code"))
            {
                int error_code = obj.value("error_code").toVariant().toInt();
                if(error_code == 0)
                {
                    if(obj.contains("result"))
                    {
                        const QJsonObject retObj = obj.value("result").toObject();
                        if(retObj.contains("data"))
                        {
                            const QJsonObject dataObj = retObj.value("data").toObject();
                            if(dataObj.contains("avoid"))
                            {
                                //忌
                                QString avoidStr = dataObj.value("avoid").toString();
                                QString avoidRet = avoidStr.split('.').join('\n');
                                ui->label_9->setText(avoidRet);
                            }

                            if(dataObj.contains("suit"))
                            {
                                //宜
                                QString suitStr = dataObj.value("suit").toString();
                                QString suitRet = suitStr.split('.').join('\n');
                                ui->label_7->setText(suitRet);

                            }
                        }
                    }
                }
                else
                {
                    qDebug() << "error_code = " << error_code;
                }
            }
        }
    }
    else
    {
        qDebug() << "errorString = " << json_error.errorString();
    }
}

void Calendar::on_pushButton_clicked()
{
    close();
}

void Calendar::mousePressEvent(QMouseEvent *event)
{
    m_startPos = event->pos();
    m_bPress = true;
    QWidget::mousePressEvent(event);
}

void Calendar::mouseMoveEvent(QMouseEvent *event)
{
    if(m_bPress)
    {
        QPoint pos = QCursor::pos() - m_startPos;
        this->move(pos);
    }
    QWidget::mouseMoveEvent(event);
}

void Calendar::mouseReleaseEvent(QMouseEvent *event)
{
    m_bPress = false;
    QWidget::mouseReleaseEvent(event);
}

void Calendar::on_toolButton_3_clicked()
{
    m_nYear = QDate::currentDate().year();
    m_nMonth = QDate::currentDate().month();
    m_nDay = QDate::currentDate().day();
    JumpTo(m_nYear, m_nMonth, m_nDay);
}

void Calendar::on_toolButton_2_clicked()
{
    //下年
    m_nYear = (12 == m_nMonth ? m_nYear + 1 : m_nYear);
    //下月
    m_nMonth = (12 == m_nMonth ? 1 : m_nMonth + 1);
    //下月天数
    int nMonthDays = Date::getMonthDays(m_nYear, m_nMonth);
    m_nDay = m_nDay > nMonthDays ? nMonthDays : m_nDay;

    JumpTo(m_nYear, m_nMonth, m_nDay);
}

void Calendar::on_toolButton_clicked()
{
    //月左翻页
    //上年
    m_nYear = (1 == m_nMonth ? m_nYear - 1 : m_nYear);
    //上月
    m_nMonth = (1 == m_nMonth ? 12 : m_nMonth - 1);
    //上月天数
    int nMonthDays = Date::getMonthDays(m_nYear, m_nMonth);
    m_nDay = m_nDay > nMonthDays ? nMonthDays : m_nDay;

    JumpTo(m_nYear, m_nMonth, m_nDay);
}

void Calendar::on_comboBox_2_currentIndexChanged(int index)
{
    Q_UNUSED(index);
//    m_nMonth = index + 1;
//    int nMonthDays = Date::getMonthDays(m_nYear, m_nMonth);
//    m_nDay = m_nDay > nMonthDays ? nMonthDays : m_nDay;

//    JumpTo(m_nYear, m_nMonth, m_nDay);
}

void Calendar::on_comboBox_currentIndexChanged(int index)
{
    Q_UNUSED(index);
//    m_nMonth = 2000 + index;
//    int nMonthDays = Date::getMonthDays(m_nYear, m_nMonth);
//    m_nDay = m_nDay > nMonthDays ? nMonthDays : m_nDay;

//    JumpTo(m_nYear, m_nMonth, m_nDay);
}

void Calendar::on_comboBox_2_activated(int index)
{
    //月选择
    m_nMonth = index + 1;
    int nMonthDays = Date::getMonthDays(m_nYear, m_nMonth);
    m_nDay = m_nDay > nMonthDays ? nMonthDays : m_nDay;

    JumpTo(m_nYear, m_nMonth, m_nDay);
}

void Calendar::on_comboBox_activated(int index)
{
    //年选择
    m_nMonth = 2000 + index;
    int nMonthDays = Date::getMonthDays(m_nYear, m_nMonth);
    m_nDay = m_nDay > nMonthDays ? nMonthDays : m_nDay;

    JumpTo(m_nYear, m_nMonth, m_nDay);
}

void Calendar::on_comboBox_3_activated(int index)
{
    switch(index)
    {
        case 1:
        {
            //元旦节
            m_nMonth = 1;
            m_nDay = 1;
            JumpTo(m_nYear, m_nMonth, m_nDay);
        }
        break;
        case 2:
        {
            //除夕
            CDate date = Date::FindChuxi(m_nYear);
            m_nYear = date.nYear;
            m_nMonth = date.nMonth;
            m_nDay = date.nDay;
            JumpTo(m_nYear, m_nMonth, m_nDay);
        }
        break;
        case 3:
        {
            //春节
            CDate date = Date::FindChunjie(m_nYear);
            m_nYear = date.nYear;
            m_nMonth = date.nMonth;
            m_nDay = date.nDay;
            JumpTo(m_nYear, m_nMonth, m_nDay);
        }
        break;
        case 4:
        {
            //清明节
            CDate date = Date::FindQingming(m_nYear);
            m_nYear = date.nYear;
            m_nMonth = date.nMonth;
            m_nDay = date.nDay;
            JumpTo(m_nYear, m_nMonth, m_nDay);
        }
        break;
        case 5:
        {
            //劳动节
            m_nMonth = 5;
            m_nDay = 1;
            JumpTo(m_nYear, m_nMonth, m_nDay);
        }
        break;
        case 6:
        {
            //端午节
            CDate date = Date::FindDuanwu(m_nYear);
            m_nYear = date.nYear;
            m_nMonth = date.nMonth;
            m_nDay = date.nDay;
            JumpTo(m_nYear, m_nMonth, m_nDay);
        }
        break;
        case 7:
        {
            //中秋节
            CDate date = Date::FindZhongqiu(m_nYear);
            m_nYear = date.nYear;
            m_nMonth = date.nMonth;
            m_nDay = date.nDay;
            JumpTo(m_nYear, m_nMonth, m_nDay);
        }
        break;
        case 58:
        {
            //劳动节
            m_nMonth = 10;
            m_nDay = 1;
            JumpTo(m_nYear, m_nMonth, m_nDay);
        }
        break;
    }
}
